//进行API统一管理
import requests from './request';
import access from "@/api/access";
import electricity from "@/api/electricity";
import kakou from "@/api/kakou";
// 组件B
import bus from '@/utils/bus'

// 创建异步函数来获取 token
async function getToken() {
    return new Promise((resolve) => {
        bus.$on('token', (token) => {
            // 使用 token 值来解析这个 Promise
            resolve(token);
        });
    });
}
//组织接口
export async  function getPage() {
    try {
        // 获取 token
        let token = await getToken();
        // console.log(token,'getTokengetToken')
        // 在请求头部使用 token
        return await access({
            url: '/evo-apigw/evo-brm/1.2.0/organization/subsystem/page',
            method: 'post',
            data: {
                "pageNum": 1,
                "pageSize": 100,
                "includeSubOrgCodeFlag": true
            },
            headers: {
                "X-H3C-ID": "defaultWorkspace",
                "X-H3C-APPKEY": "c21f969b",
                "Content-Type": "application/json",
                "Authorization": "bearer " + token
                // "Authorization": "bearer 1:b563a413-b667-4a9e-b61c-8b7da4230a42"
            },
        });
    } catch (error) {
        // 处理可能出现的错误
        console.error("获取数据时出错：", error);
        throw error;
    }
}

export async  function getMent() {
    try {
        // 获取 token
        let token = await getToken();
        // 在请求头部使用 token
        return await access({
            url: '/evo-apigw/evo-brm/1.2.0/device/channel/page',
            method: 'get',
            headers: {
                "Authorization": "bearer " + token
                // "Authorization": "bearer 1:195d780c-6a91-47be-b595-d0161d7ce8e2"
            },
        });
    } catch (error) {
        // 处理可能出现的错误
        console.error("获取数据时出错：", error);
        throw error;
    }
}
// export function getPage() {
//     return requests({
//         url: '/organization/subsystem/page',
//         method: 'post',
//         data: {
//             "pageNum": 1,
//             "pageSize": 100,
//             "includeSubOrgCodeFlag": true
//         },
//         headers: {
//             "X-H3C-ID": "defaultWorkspace",
//             "X-H3C-APPKEY": "c21f969b",
//             "Content-Type": "application/json",
//             "Authorization": token
//         },
//     })
// }
//用电统计、用水统计、用气统计
export function getStatistics() {
    return requests({
        url: '/api/electricity/consumption',
        method: 'get',
        headers: {
            'Content-Type': 'application/json', // 请求参数格式为 JSON
        },
    });
}

// export function getEnergy(params) {
//     return requests({
//         url: '/api/device/item/data',
//         method: 'get',
//         params: params,
//         headers: {
//             'X-H3C-ID': 'defaultWorkspace',
//             'X-H3C-APPKEY': 'c21f969b',
//             'Content-Type': 'application/json', // 请求参数格式为 JSON
//             'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
//         },
//     });
// }
export function getEnergy(params) {
    return requests({
        url: '/api/electricity/tendency',
        method: 'get',
        params: params,
        headers: {
            'Content-Type': 'application/json',
        },
    })
}

// 图片token
export function getAuth(params) {
    return requests({
        url: '/userCertification/userPasswordAuth',
        method: 'get',
        params: params,
        headers: {
            'Content-Type': 'application/json',
        },
    })
}
//能耗点位接口
export function getElectricity(params) {
    return electricity({
        url: '/api/deviceservice/api/electricity/hazard-data',
        method: 'get',
        params: params,
        headers: {
            'Content-Type': 'application/json',
        },
    })
}
export function getElectricitys(params) {
    return electricity({
        url: '/api/deviceservice/api/device/item/data',
        method: 'get',
        params: params,
        headers: {
            'Content-Type': 'application/json',
        },
    })
}
//企业年度用电
export function getAnnual(params) {
    return electricity({
        url: '/api/deviceservice/api/electricity/annual-consumption-ratio',
        method: 'get',
        params: params,
        headers: {
            'Content-Type': 'application/json',
        },
    })
}
export function getPower(params) {
    return electricity({
        url: '/api/deviceservice/api/electricity/power-comparison',
        method: 'get',
        params: params,
        headers: {
            'Content-Type': 'application/json',
        },
    })
}


